<template>
  <div class="container">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      error-text="加载失败，请稍后重试"
      :error="error"
    >
      <fans-item
        :active="active"
        v-for="(item,index) in list"
        :key="index"
        :data="item"
      >
      </fans-item>
    </van-list>
  </div>
</template>
<script>
import { getUserFans, getUserUp } from '@/api/user.js'
import fansItem from './fans-item'
export default {
  props: ['id', 'active'],
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      error: false
    }
  },
  components: {
    fansItem
  },
  methods: {
    async onLoad () {
      if (this.active === 0) {
        try {
          const { data: res } = await getUserUp({
            id: this.id,
            page: this.page
          })
          this.list.push(...res.data.results)
          this.page++
          if (res.data.results.length === 0) {
            this.finished = true
          }
        } catch (err) {
          this.error = true
        }
        this.loading = false
      } else {
        try {
          const { data: res } = await getUserFans({
            id: this.id,
            page: this.page
          })
          this.list.push(...res.data.results)
          this.page++
          if (res.data.results.length === 0) {
            this.finished = true
          }
        } catch (err) {
          this.error = true
        }
        this.loading = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.container {
}
</style>
